package com.javaweb.cwj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javaweb.cwj.entity.EduCourse;
import com.javaweb.cwj.entity.EduCourseDescription;
import com.javaweb.cwj.entity.vo.CourseInfoVo;
import com.javaweb.cwj.exception.cwjException;
import com.javaweb.cwj.mapper.EduCourseMapper;
import com.javaweb.cwj.service.EduCourseDescriptionService;
import com.javaweb.cwj.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-05-13
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
   @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Override
    public String addCourse(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i = baseMapper.insert(eduCourse);
        if(i==0){
            throw new cwjException(20001,"添加数据失败");
        }
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription()) ;
        eduCourseDescriptionService.addDescription(eduCourseDescription);
        return eduCourse.getId();
    }

    @Override
    public CourseInfoVo selectCourseInfo(String courseId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",courseId);
       EduCourse eduCourse =  baseMapper.selectOne(queryWrapper);
       CourseInfoVo courseInfoVo = new CourseInfoVo();
       BeanUtils.copyProperties(eduCourse,courseInfoVo);
      EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);
      courseInfoVo.setDescription(eduCourseDescription.getDescription());
        return courseInfoVo;
    }

    @Override
    public int updateCourse(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i  = baseMapper.updateById(eduCourse);
        return i;
    }
}
